package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    /**
     * 分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> pageQuer(String name, Integer status, Integer pageNum, Integer pageSize) {
        //调用分页插件
        PageHelper.startPage(pageNum, pageSize);

        Page<NursingPlanVo> planVos = nursingPlanMapper.pageQuer(name, status);
        PageResponse<NursingPlanVo> PageResponse = new PageResponse<>(planVos);
        PageResponse.setRecords(planVos.getResult());
        return PageResponse;

    }

    /**
     * 根据id删除护理计划
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingPlanMapper.deleteById(id);
        // 关联项目先删除
        nursingPlanProjectMapper.deleteByPlanId(id);
    }


    /**
     * 启用/禁用护理计划
     *
     * @param id
     * @param status
     */
    @Override
    public void startOrStopStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatusById(id, status);
    }


    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo findId(Long id) {
        return nursingPlanMapper.findId(id);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        // 插入基本信息
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);//默认禁用
        try {
            nursingPlanMapper.insert(nursingPlan);
        } catch (DuplicateKeyException e) {
            throw new BaseException(BasicEnum.NAME_ALREADY_EXIST);
        }
        // 插入关联计划项目
        // 添加关联plan_id
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans != null && projectPlans.size() > 0) {
            for (NursingProjectPlanVo projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            nursingPlanProjectMapper.insertBatch(projectPlans);
        }
    }

    /**
     * 编辑护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //基本信息直接修改
        nursingPlanMapper.update(nursingPlan);

        // 关联项目先删除
        nursingPlanProjectMapper.deleteByPlanId(nursingPlan.getId());
        // 重新插入
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans != null && projectPlans.size() > 0) {
            for (NursingProjectPlanVo projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            nursingPlanProjectMapper.insertBatch(projectPlans);
        }
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> getAll() {
        List<NursingPlan> nursingPlanList = nursingPlanMapper.getAll();
        return nursingPlanList;
    }
}
